var imgSrc = []; //图片路径
var imgFile = []; //文件流
var imgName = []; //图片名字
//选择图片
function imgUpload(obj) {

	var oInput = '#' + obj.inputId;
	var imgBox = '#' + obj.imgBox;
	var btn = '#' + obj.buttonId;
  var formdata = new FormData();
	$(oInput).on("change", function() {
		imgSrc = [];
		imgFile = [];
		imgName = [];
		var files = document.getElementById(obj.inputId).files;
    console.log(files);
    for (var i = 0; i < files.length; i++) {
			var imgSrcI = getObjectURL(files[i]);
			imgName.push(files[i].name);
			imgSrc.push(imgSrcI);
			imgFile.push(files[i]);
      formdata.append([i],files[i]);
    }
		addNewContent(imgBox);
	})
	$(btn).on('click', function() {
		/*var data = new Object;
		data = imgFile;*/
		$.ajax({
        url:obj.upUrl,
        type:"post",
        data:formdata,
        contentType:false, //- 必须false才会自动加上正确的Content-Type
        processData: false, //- 必须false才会避开jQuery对 formdata 的默认处理,XMLHttpRequest会对 formdata 进行正确的处理
        success:function(res){
            if (res.code){
                layer.msg(res.msg);
                var oldInput = "";
                for (var i = 0; i < res.data.length; i++) {
                	oldInput += '<input type="hidden" name="ProductImage['+i+']" value="'+ res.data[i].src +'">';
                	$("#imgInput").html(oldInput);
                }
            }else {
                layer.msg(res.msg)
            }
        },
        error:function(){
            layer.msg("请联系管理员");
        }
    });
		// submitPicture(obj.upUrl, data);
	})
}
//图片展示
function addNewContent(obj) {
	$(imgBox).html("");
	for(var a = 0; a < imgSrc.length; a++) {
		var oldBox = $(obj).html();
		// $(obj).html(oldBox + '<div class="imgContainer"><img title=' + imgName[a] + ' alt=' + imgName[a] + ' src=' + imgSrc[a] + ' onclick="imgDisplay(this)"><p onclick="removeImg(this,' + a + ')" class="imgDelete">删除</p></div>');
		$(obj).html(oldBox + '<div class="imgContainer"><img title=' + imgName[a] + ' alt=' + imgName[a] + ' src=' + imgSrc[a] + ' onclick="imgDisplay(this)"></div>');
	}
}
//删除
function removeImg(obj, index) {
	imgSrc.splice(index, 1);
	imgFile.splice(index, 1);
	imgName.splice(index, 1);
	var boxId = "#" + $(obj).parent('.imgContainer').parent().attr("id");
	addNewContent(boxId);
}
//上传(将文件流数组传到后台)
function submitPicture(url,data) {
	console.log(data);
	// alert('请打开控制台查看传递参数！');
	if(url&&data){
		$.ajax({
			type: "post",
			url: url,
			async: true,
			data: data,
			traditional: true,
			success:function(res){
          if (res.code){
              layer.msg("ok");
          }else {
              layer.msg(res.msg)
          }
      },
      error:function(){
          layer.msg("请联系管理员");
      }
		});
	}
}
//图片灯箱
function imgDisplay(obj) {
	var src = $(obj).attr("src");
	var imgHtml = '<div style="width: 100%;height: 100vh;overflow: auto;background: rgba(0,0,0,0.5);text-align: center;position: fixed;top: 0;left: 0;z-index: 1000;"><img src=' + src + ' style="margin-top: 100px;width: 70%;margin-bottom: 100px;"/><p style="font-size: 50px;position: fixed;top: 30px;right: 30px;color: white;cursor: pointer;" onclick="closePicture(this)">×</p></div>'
	$('body').append(imgHtml);
}
//关闭
function closePicture(obj) {
	$(obj).parent("div").remove();
}

//图片预览路径
function getObjectURL(file) {
	var url = null;
	if(window.createObjectURL != undefined) { // basic
		url = window.createObjectURL(file);
	} else if(window.URL != undefined) { // mozilla(firefox)
		url = window.URL.createObjectURL(file);
	} else if(window.webkitURL != undefined) { // webkit or chrome
		url = window.webkitURL.createObjectURL(file);
	}
	return url;
}